package com.yantong.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@SuppressWarnings("static-access")
public class HornUtil {
//    static HornUtil p = new HornUtil();
    public static final String URL = "http://www.quiertech.com";// 不需要修改
    public static final String KEY = "20190401140932202019040114093258";// 不需要修改



    //获取喇叭token
    public static String getToken(String deviceId) {
        String content = "";
        String signStr = "deviceId=" + deviceId + "&key=" + KEY;
        String sign = MD5Utils.get32Md5(signStr);
        Map contentMap = new HashMap();//必传参数
        contentMap.put("deviceId", deviceId);
        contentMap.put("sign", sign);

        JSONObject json = new JSONObject(contentMap);
        content = json.toString();
        System.out.println("param ==>" + content);

        String url = URL + "/inner/getTokenByDeviceId";
        String returnStr = null;
        try {
            returnStr = HttpUtil.sendPost(url, content);
        }  catch (IOException e) {
            e.printStackTrace();
        }

        Map mapTypes = JSON.parseObject(returnStr);
        System.out.println("result ==>" + mapTypes);

        if(mapTypes.containsKey("code")){
            if(mapTypes.get("code").toString().equals("0")){
                Map dataMap = JSON.parseObject(mapTypes.get("data").toString());
                if(dataMap.containsKey("token")){
                    return dataMap.get("token").toString();
                }
            }
        }
        return null;
    }

    //查询喇叭状态
    public static String getDeviceInfo(String deviceId, String token) {
        String content = "";
        String signStr = "id=" + deviceId + "&token=" + token + "&key=" + KEY;
        String sign = MD5Utils.get32Md5(signStr);
        Map contentMap = new HashMap();//必传参数
        contentMap.put("id", deviceId);
        contentMap.put("sign", sign);
        contentMap.put("token", token);

        JSONObject json = new JSONObject(contentMap);
        content = json.toString();
        System.out.println("param ==>" + content);

        String url = URL + "/cloundPay/getDeviceInfo";
        String returnStr = null;
        try {
            returnStr = HttpUtil.sendPost(url, content);
        }  catch (IOException e) {
            e.printStackTrace();
        }

        Map mapTypes = JSON.parseObject(returnStr);
        System.out.println("result ==>" + mapTypes);
        //{"msg":"成功","code":"0","data":{"playend":"","playheader":"","online":"1","battery":"100","deviceName":"2G喇叭"}}
        //0-在线 1-其他
        if(mapTypes.containsKey("code")){
            if(mapTypes.get("code").toString().equals("0")){
                Map dataMap = JSON.parseObject(mapTypes.get("data").toString());
                if(dataMap.containsKey("online")){
                    return dataMap.get("online").toString();
                }
            }
        }

        return "1";

    }

//    public static void main(String[] args) {
//        push("G402542",getToken("G402542"),"1000","D20200721071437250564","10","哈哈哈哈");
//    }

    public static void main(String[] args) throws Exception{
//        System.out.println(getToken("G402542"));
//        getToken("G402542");
//        getDeviceInfo("G402542", "c56dd6b65b3f4a36886a1936356d8aaf");
        System.out.println(push("G402542", "c56dd6b65b3f4a36886a1936356d8aaf", "1000000", "", "10", "琰掌柜到账123123.34元"));
        System.out.println(push("G402542", "c56dd6b65b3f4a36886a1936356d8aaf", "1000000", "", "10", "琰掌柜到账13123.34元"));
        System.out.println(push("G402542", "c56dd6b65b3f4a36886a1936356d8aaf", "1000000", "", "10", "琰掌柜到账12323.4元"));
        System.out.println(push("G402542", "c56dd6b65b3f4a36886a1936356d8aaf", "1000000", "", "10", "琰掌柜到账1223.34元"));
        System.out.println(push("G402542", "c56dd6b65b3f4a36886a1936356d8aaf", "1000000", "", "10", "琰掌柜到账12313.34元"));
    }

    public static String push(String deviceId, String token, String price, String traeNo, String pt, String pricetts) {
        String content = "";
        Map contentMap = new HashMap();//必传参数
        String urlParams = "";
        urlParams += "id=" + deviceId;
        urlParams += "&token=" + token;
        urlParams += "&price=" + price;
        urlParams += "&trace_no=" + traeNo;
        urlParams += "&vol=80";
        urlParams += "&seq=" + getRandomNum(10);
        urlParams += "&pt=" + pt;
        if(pt.equals("10") && StringUtils.isNotEmpty(pricetts)){
            urlParams += "&pricetts=" + pricetts;
        }


        JSONObject json = new JSONObject(contentMap);
        content = json.toString();
        System.out.println("param ==>" + content);

        String url = URL + "/cloundPay/push?" + urlParams;
        String returnStr = null;
        try {
            returnStr = HttpUtil.sendPost(url,content);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Map mapTypes = JSON.parseObject(returnStr);
        System.out.println("result ==>" + mapTypes);
        //{"msg":"成功","code":"0","data":{"playend":"","playheader":"","online":"1","battery":"100","deviceName":"2G喇叭"}}
        if(mapTypes.containsKey("code")){
            return mapTypes.get("code").toString();
        }
        return "-1";
    }

    /**
     * 获取随机字符串
     *
     * @param num
     * @return
     */
    public static String getRandomNum(Integer num) {
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < num; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

}